package component.fulltextsearch.index;

import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;

import component.fulltextsearch.constant.SearchConstant;
import component.fulltextsearch.entity.CommonUserDocument;
import component.fulltextsearch.entity.UserDocument;
import component.fulltextsearch.util.AnalyzerUtil;
import component.fulltextsearch.util.IndexFactoryUtil;

public class SimpleIndexOperate implements IIndexOperate {
	
	private Analyzer analyzer = AnalyzerUtil.getAnalyzer();

	public boolean createCommonIndex(CommonUserDocument commonUserDoc) {
		return this.createIndex(commonUserDoc.getFinalUserDocuments());
	}

	public boolean createCommonIndex(Set commonUserDocSet) {
		Set set = new HashSet();
		for (Iterator it = commonUserDocSet.iterator(); it.hasNext();) {
			CommonUserDocument commonUserDoc = (CommonUserDocument) it.next();
			set.addAll(commonUserDoc.getFinalUserDocuments());
		}
		return this.createIndex(set);
	}

	public boolean createIndex(UserDocument userDoc) {
		Set set = new HashSet();
		set.add(userDoc);
		return this.createIndex(set);
	}

	public boolean createIndex(Set userDocSet) {
		DocumentTransform trans = new DocumentTransform();
		IndexWriter writer = null;
		try {
			writer = IndexFactoryUtil.getInstance().getIndexWriter();
			for (Iterator it = userDocSet.iterator(); it.hasNext();) {
				UserDocument userDoc = (UserDocument) it.next();
				Document  document = trans.transform(userDoc);
				writer.addDocument(document, analyzer);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			try {
				IndexFactoryUtil.getInstance().closeWriter(writer);			
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return true;
	}

	public boolean deleteIndex(String keyValue) {
		Set set = new HashSet();
		set.add(keyValue);
		return this.deleteIndex(set);
	}

	public boolean deleteIndex(Set keyValueSet) {
		IndexReader reader = null;
		try {
			reader = IndexFactoryUtil.getInstance().getIndexReader();
			for (Iterator it = keyValueSet.iterator(); it.hasNext();) {
				String keyValue = (String) it.next();
				Term term = new Term(SearchConstant.NAME_KEYVALUE, keyValue);
				int i = reader.deleteDocuments(term);
				//System.out.println("删除了 " + i + " 条索引");
			}
		} catch (IOException ex) {
			ex.printStackTrace();
			return false;
		}finally{
			try {
				IndexFactoryUtil.getInstance().closeReader(reader);
			}catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return true;
	}
	
	public boolean deleteAllIndex() {
		IndexReader reader = null;
		try {
			reader = IndexFactoryUtil.getInstance().getIndexReader();
			Term term = new Term(SearchConstant.NAME_ISDELETE, SearchConstant.DELETE_FLAG);
			int i = reader.deleteDocuments(term);
			System.out.println("删除了 " + i + " 条索引");
		} catch (IOException ex) {
			ex.printStackTrace();
			return false;
		}finally{
			try {
				IndexFactoryUtil.getInstance().closeReader(reader);
			}catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return true;
	}

	public boolean updateCommonIndex(CommonUserDocument commonUserDoc) {
		return this.updateIndex(commonUserDoc.getFinalUserDocuments());
	}

	public boolean updateCommonIndex(Set commonUserDocSet) {
		for (Iterator it = commonUserDocSet.iterator(); it.hasNext();) {
			CommonUserDocument commonUserDoc = (CommonUserDocument)it.next();
			this.updateIndex(commonUserDoc.getFinalUserDocuments());
		}
		return true;
	}

	public boolean updateIndex(UserDocument userDoc) {
		String keyValue = userDoc.getKeyValue();
		this.deleteIndex(keyValue);
		userDoc.setKeyValue(keyValue);
		return this.createIndex(userDoc);
	}

	public boolean updateIndex(Set userDocSet) {
		Set keyValueSet = new HashSet();
		for (Iterator it = keyValueSet.iterator(); it.hasNext();) {
			UserDocument userDoc = (UserDocument)it.next();
			String keyValue = userDoc.getKeyValue();
			keyValueSet.add(keyValue);
		}
		this.deleteIndex(keyValueSet);
		
		return this.createIndex(userDocSet);
	}

}
